#!/usr/bin/env python
# -*- coding: latin1 -*-

# TruckPC v0.1
# Author: Peumans Joris
# April 2008

# development of Octopus stopped 2008/4/28

# to do list:
# - logging of all the data
# ... warning of disk almost full
# - finalise GPS probe (calculate distance in time intervals)
# - implement plus>min 



# load basic library
import sys

#check if Octopus or Arduino is connected
f = open("/proc/bus/usb/devices")
CheckOctopus = False
CheckArduino = False
for line in f:
    if line[0:22] == "S:  Product=OctopusUSB":
        CheckOctopus = True
    if line[0:27] == "S:  Product=FT232R USB UART":
        CheckArduino = True

if not CheckOctopus and not CheckArduino:
   print "No Octopus or Arduino connected."
   sys.exit()



# load rest of libraries
import os
import shutil
import gtk.glade
import pango
import pygtk
import thread
import time
import gobject
import serial

if CheckOctopus:
   from  octopus import *

# standard fonts
FontButton = "Freesans 38"
FontHeader = "Freesans 24"
FontButtonSmall = "Freesans 32"
FontButtonNumber = "FFF Forward 92"
FontFigure = "FFF Forward 92"
FontDescription = "Freesans 18"
FontUnity = "Freesans 18"
FontLog = "Freesans 22"
FontLabel = "Freesans 14"
FontGPSBig = "FFF Forward 62"
FontGPSSmall = "Freesans 38"

# standard values
FileSettings = "truckpc.ini"
FileGlade = "truckpc.glade"
KeyboardTimeOld = time.time()
KeyboardTimeNew = time.time()
SoundTimeOld = time.time()
SoundTimeNew = time.time()
GPSTimeNewGGA = time.time()
GPSTimeOldGGA = time.time()
GPSTimeNewRMC = time.time()
GPSTimeOldRMC = time.time()

# to count difference between GPS points for GPS Probe
GPSTimeNewLL = time.time() 
GPSTimeOldLL = time.time()

SpeedTimeOld = time.time()
SpeedTimeNew = time.time()
Log = ["","","","","","","","","",""]
Speed = 0.0
GPSBuffer = ""
GPSCourseOld = "-- (degrees)"
ArduinoBlocked = False

BasicWindow = "WT"
ScreenWidth = 800
ScreenHeight = 600
Calibration = "1"
Calibration1 = 2448
Calibration2 = 1111
Interval = 0
Total = 0
SuperTotal = 0
DelayKeyboard = 0.5   
Probe = "1"
PlusMin = "Plus>Min"
Display = "Display I/S"
DelaySound = 2
SoundWarning = "bark.au"
SoundReset = "bark.au"
DeviceGPS = "/dev/ttyS0"
BaudrateGPS = 4800
UnityGPS = "degrees"  # or "decimals"
GWT=2
DeviceArduino = "/dev/ttyUSB0"
BaudrateArduino = 19200
DelayGPS = 2 # for difference between GPS points
DelaySpeed = 2 # timeframe for calculating speed trucktrip

GPSTime = ""
GPSTime_ = 0
GPSDate = ""
GPSDate_ = 0
GPSLatitude = ""
GPSLatitude_ = 0
GPSLatitudeOld_ = 0
GPSLongitude = ""
GPSLongitude_ = 0
GPSLongitudeOld_ = 0
GPSNumberSattelites = ""
GPSNumberSattelites_ = 0
GPSSpeed = ""
GPSSpeed_ = 0
GPSHeight = ""
GPSHeight_ = 0
GPSCourse = ""
GPSCourse_ = 0
GPSColorGGA = "white"
GPSColorRMC = "white"

class TruckPC:
   def ReadSettings(self, FileName):
    global BasicWindow
    global ScreenWidth
    global ScreenHeight
    global Calibration
    global Calibration1
    global Calibration2
    global Interval
    global Total
    global SuperTotal
    global DelayKeyboard
    global Probe
    global PlusMin
    global Display
    global DelaySound
    global SoundWarning
    global SoundReset
    global DeviceGPS
    global BaudrateGPS
    global UnityGPS
    global GWT
    global DeviceArduino
    global BaudrateArduino
    global DelayGPS
    global DelaySpeed

    if not os.path.exists(FileName):
       print "INI file not found"
       sys.exit()

    f = open(FileName)
    line = f.readline()
    while line <> "":
        while line[0] == "#":
              line = f.readline()
        if line[0: 9] == "_warnings":
           line = f.readline()
           while line[0] == "#":
                 line = f.readline()
           while line <> "" and line[0] <> "_":
                 partstring = ""
                 self.Mini_Array =[]
                 for letter in line:
                     if letter == "," or letter == "\n":
                        self.Mini_Array.append(partstring)
                        partstring = ""
                     else:
                        partstring = partstring + letter
                 self.Mini_Array.append(partstring)
                 Warnings.append(self.Mini_Array)
                 line = f.readline()
           continue
        if line[0: 6] == "_ports":
           line = f.readline()
           while line[0] == "#":
                 line = f.readline()
           while line <> "" and line[0] <> "_":
                 partstring = ""
                 self.Mini_Array =[]
                 for letter in line:
                     if letter == "\n":
                        self.Mini_Array.append(partstring)
                        partstring = 0
                     elif letter == ",":
                        self.Mini_Array.append(partstring)
                        partstring = ""
                     else:
                        partstring = partstring + letter
                 self.Mini_Array.append(partstring)
                 Ports.append(self.Mini_Array)
                 line = f.readline()
           continue
        if line[0: 12] == "_conversions":
           line = f.readline()
           while line[0] == "#":
                 line = f.readline()
           while line <> "" and line[0] <> "_":
                 partstring = ""
                 self.Mini_Array =[]
                 for letter in line:
                     if letter == "," or letter == "\n":
                        self.Mini_Array.append(partstring)
                        partstring = ""
                     else:
                        partstring = partstring + letter
                 self.Mini_Array.append(partstring)
                 Conversions.append(self.Mini_Array)
                 line = f.readline()
        if line[0:14] == "_basicsettings":
           line = f.readline()
           while line[0] == "#":
                 line = f.readline()
           while line <> "" and line[0] <> "_":
                 if line.startswith("BasicWindow"):
                    BasicWindow = line.partition("=")[2].rstrip("\n")
                 if line.startswith("ScreenWidth"):
                    ScreenWidth = int(line.partition("=")[2])
                 if line.startswith("ScreenHeight"):
                    ScreenHeight = int(line.partition("=")[2])
                 if line.startswith("Calibration"):
                    if line.startswith("Calibration1"):
                       Calibration1 = int(line.partition("=")[2].rstrip("\n"))
                    elif line.startswith("Calibration2"):
                         Calibration2 = int(line.partition("=")[2].rstrip("\n"))
                    else:
                         Calibration = line.partition("=")[2].rstrip("\n")
                 if line.startswith("Interval"):
                    Interval = float(line.partition("=")[2].rstrip("\n"))
                 if line.startswith("Total"):
                    Total = float(line.partition("=")[2].rstrip("\n"))
                 if line.startswith("SuperTotal"):
                    SuperTotal = float(line.partition("=")[2].rstrip("\n"))
                 if line.startswith("DelayKeyBoard"):
                    # seconds, to prevent repeatingkeys in all window because of touchscreen
                    DelayKeyboard = float(line.partition("=")[2])
                 if line.startswith("Probe"):
                    Probe = line.partition("=")[2].rstrip("\n")
                 if line.startswith("PlusMin"):
                    PlusMin = line.partition("=")[2].rstrip("\n")
                 if line.startswith("Display"):
                    Display = line.partition("=")[2].rstrip("\n")
                 if line.startswith("DelaySound"):
                    DelaySound = float(line.partition("=")[2].rstrip("\n"))
                 if line.startswith("SoundWarning"):
                    SoundWarning = line.partition("=")[2].rstrip("\n")
                 if line.startswith("SoundReset"):
                    SoundReset = line.partition("=")[2].rstrip("\n")
                 if line.startswith("DeviceGPS"):
                    DeviceGPS = line.partition("=")[2].rstrip("\n")
                 if line.startswith("BaudrateGPS"):
                    BaudrateGPS = int(line.partition("=")[2].rstrip("\n"))
                 if line.startswith("UnityGPS"):
                    UnityGPS = line.partition("=")[2].rstrip("\n")
                 if line.startswith("GWT"):
                    GWT = int(line.partition("=")[2])
                 if line.startswith("DeviceArduino"):
                    DeviceArduino = line.partition("=")[2].rstrip("\n")
                 if line.startswith("BaudrateArduino"):
                    BaudrateArduino = int(line.partition("=")[2].rstrip("\n"))
                 if line.startswith("DelayGPS"):
                    DelayGPS = float(line.partition("=")[2].rstrip("\n"))
                 if line.startswith("DelaySpeed"):
                    DelaySpeed = float(line.partition("=")[2].rstrip("\n"))
                 line = f.readline()
           continue
        line = f.readline()
    f.close

#_ports (0=code,1=description,2=unity,3=port,4=type,5=multiplicator,6=screen,7=value)
   def GetBlock(self, i):
    for line_ports in Ports:
        if line_ports[6] == str(i):
           if line_ports[2] == "bar":
              return (['%.1f' % float(line_ports[7]),line_ports[1],line_ports[2]])
           else:
              if line_ports[2] == "C" or line_ports[2] == "percent":
                 return ([str(int(line_ports[7])),line_ports[1],line_ports[2]])
              else:
                 return ([str(line_ports[7]),line_ports[1],line_ports[2]])
    return (["","",""])
                
   def GetColor(self, i):
    global SoundWarning
    global SoundTimeNew
    global SoundTimeOld
    SensorCode = ""
    SensorValue = 0
    SensorDigital = False

    for line_ports in Ports:
        if line_ports[6] == str(i):
           SensorCode = line_ports[0]
           if line_ports[4] == "digital":
              SensorDigital = True
              SensorValue = line_ports[7]
           else:
              SensorDigital = False
              SensorValue = float(line_ports[7])

    for line_warnings in Warnings:
        if SensorDigital:
           if SensorCode == line_warnings[0] and line_warnings[1] == SensorValue:
              if line_warnings[4] == "beep":
                 SoundTimeNew=time.time()
                 if (SoundTimeNew - SoundTimeOld) > DelaySound:
                    SoundTimeOld = SoundTimeNew
                    os.system("aplay -q "+SoundWarning)
              return line_warnings[3]
        else:   
           if SensorCode == line_warnings[0] and float(line_warnings[1]) <= SensorValue and SensorValue<= float(line_warnings[2]):
              if line_warnings[4] == "beep":
                 SoundTimeNew=time.time()
                 if (SoundTimeNew - SoundTimeOld) > DelaySound:
                    SoundTimeOld = SoundTimeNew
                    os.system("aplay -q "+SoundWarning)
              return line_warnings[3]
    return "white"
    
   def SafeBasicSettings(self):
    f = open(FileSettings)
    s = file(FileSettings+".backup", "w")    
    line = f.readline()
    while line <> "":
          s.writelines(line)
          if line[0:14] == "_basicsettings":
             s.writelines("BasicWindow="+BasicWindow+"\n")
             s.writelines("ScreenWidth="+str(ScreenWidth)+"\n")
             s.writelines("ScreenHeight="+str(ScreenHeight)+"\n")
             s.writelines("Calibration="+Calibration+"\n")
             s.writelines("Calibration1="+str(int(Calibration1))+"\n")
             s.writelines("Calibration2="+str(int(Calibration2))+"\n")
             s.writelines("Interval="+str(Interval)+"\n")
             s.writelines("Total="+str(Total)+"\n")
             s.writelines("SuperTotal="+str(SuperTotal)+"\n")
             s.writelines("DelayKeyboard="+str(DelayKeyboard)+"\n")
             s.writelines("Probe="+Probe+"\n")
             s.writelines("PlusMin="+PlusMin+"\n")
             s.writelines("Display="+Display+"\n")
             s.writelines("DelaySound="+str(DelaySound)+"\n")
             s.writelines("SoundWarning="+SoundWarning+"\n")
             s.writelines("SoundReset="+SoundReset+"\n")
             s.writelines("DeviceGPS="+DeviceGPS+"\n")
             s.writelines("BaudrateGPS="+str(BaudrateGPS)+"\n")
             s.writelines("UnityGPS="+UnityGPS+"\n")
             s.writelines("GWT="+str(GWT)+"\n")
             s.writelines("DeviceArduino="+DeviceArduino+"\n")
             s.writelines("BaudrateArduino="+str(BaudrateArduino)+"\n")
             s.writelines("DelayGPS="+str(DelayGPS)+"\n")
             s.writelines("DelaySpeed="+str(DelaySpeed)+"\n")
             break
          line = f.readline()
    s.flush()
    f.close
    s.close
    shutil.copy2(FileSettings+".backup",FileSettings)

class ShowWindow:
   StopApplication = False
   def __init__(self):
    
    global Interval
    global Total
    global Display
    global PlusMin

    if not os.path.exists(FileGlade):
       print "GLADE file not found"
       sys.exit()
    wTree = gtk.glade.XML(FileGlade)

# WS
    self.WS = wTree.get_widget("WindowSensors")
    self.WS.connect( "delete_event", self.QuitProgram)
    self.WS.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
    self.WS.set_resizable(False)
    self.WS.set_geometry_hints(None, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, -1, -1, -1, -1)

    self.WSHeader = wTree.get_widget("WSHeader")

    self.meter1 = wTree.get_widget("label11")
    self.meter1.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter1textleft = wTree.get_widget("label12")
    self.meter1textleft.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter1textright = wTree.get_widget("label13")
    self.meter1textright.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter2 = wTree.get_widget("label21")
    self.meter2.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter2textleft = wTree.get_widget("label22")
    self.meter2textleft.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter2textright = wTree.get_widget("label23")
    self.meter2textright.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter3 = wTree.get_widget("label31")
    self.meter3.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter3textleft = wTree.get_widget("label32")
    self.meter3textleft.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter3textright = wTree.get_widget("label33")
    self.meter3textright.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter4 = wTree.get_widget("label41")
    self.meter4.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter4textleft = wTree.get_widget("label42")
    self.meter4textleft.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter4textright = wTree.get_widget("label43")
    self.meter4textright.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter5 = wTree.get_widget("label51")
    self.meter5.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter5textleft = wTree.get_widget("label52")
    self.meter5textleft.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter5textright = wTree.get_widget("label53")
    self.meter5textright.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter6 = wTree.get_widget("label61")
    self.meter6.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter6textleft = wTree.get_widget("label62")
    self.meter6textleft.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.meter6textright = wTree.get_widget("label63")
    self.meter6textright.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))

    self.WSButtonSettings = wTree.get_widget("WSButtonSettings")
    self.WSButtonSettings.connect("pressed",self.WSButtonSettings_clicked_cb,"")
    self.WSButtonSettings.child.modify_font(pango.FontDescription(FontButton))

    self.WSButtonTrucktrip = wTree.get_widget("WSButtonTrucktrip")
    self.WSButtonTrucktrip.connect("pressed",self.WSButtonTrucktrip_clicked_cb,"")
    self.WSButtonTrucktrip.child.modify_font(pango.FontDescription(FontButton))
    self.WSButtonTrucktrip.child.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("red"))

    self.WSButtonGPS = wTree.get_widget("WSButtonGPS")
    self.WSButtonGPS.connect("pressed",self.WSButtonGPS_clicked_cb,"")
    self.WSButtonGPS.child.modify_font(pango.FontDescription(FontButton))

    self.WSButtonGPSSC = wTree.get_widget("WSButtonGPSSC")
    self.WSButtonGPSSC.connect("pressed",self.WSButtonGPSSC_clicked_cb,"")
    self.WSButtonGPSSC.child.modify_font(pango.FontDescription(FontButton))


# WSS
   
    self.WSS = wTree.get_widget("WindowSensorsSettings")
    self.WSS.connect( "delete_event", self.QuitProgram)
    self.WSS.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
    self.WSS.set_resizable(False)
    self.WSS.set_geometry_hints(None, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, -1, -1, -1, -1)

    self.WSSButtonExit = wTree.get_widget("WSSButtonExit")
    self.WSSButtonExit.connect("pressed",self.WSSButtonExit_clicked_cb,"")
    self.WSSButtonExit.child.modify_font(pango.FontDescription(FontButton))
    
    self.WSSButtonBack = wTree.get_widget("WSSButtonBack")
    self.WSSButtonBack.connect("pressed",self.WSSButtonBack_clicked_cb,"")
    self.WSSButtonBack.child.modify_font(pango.FontDescription(FontButton))

    self.WSSButtonUnityGPS = wTree.get_widget("WSSButtonUnityGPS")
    self.WSSButtonUnityGPS.connect("pressed",self.WSSButtonUnityGPS_clicked_cb,"")
    self.WSSButtonUnityGPS.child.modify_font(pango.FontDescription(FontButton))

# WT
   
    self.WT = wTree.get_widget("WindowTrucktrip")
    self.WT.connect( "delete_event", self.QuitProgram)
    self.WT.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
    self.WT.set_resizable(False)
    self.WT.set_geometry_hints(None, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, -1, -1, -1, -1)

    self.WTHeader = wTree.get_widget("WTHeader")

    self.WTButtonTop = wTree.get_widget("WTButtonTop")
    self.WTButtonTop.connect("pressed",self.WTButtonTop_clicked_cb,"")
    self.WTButtonTop.child.modify_font(pango.FontDescription(FontButtonSmall))

    self.WTButtonBottom = wTree.get_widget("WTButtonBottom")
    self.WTButtonBottom.connect("pressed",self.WTButtonBottom_clicked_cb,"")
    self.WTButtonBottom.child.modify_font(pango.FontDescription(FontButtonSmall))

    self.WTButtonDisplay = wTree.get_widget("WTButtonDisplay")
    self.WTButtonDisplay.set_label(Display)
    self.WTButtonDisplay.connect("pressed",self.WTButtonDisplay_clicked_cb,"")
    self.WTButtonDisplay.child.modify_font(pango.FontDescription(FontButton))

    self.WTButtonSensors = wTree.get_widget("WTButtonSensors")
    self.WTButtonSensors.connect("pressed",self.WTButtonSensors_clicked_cb,"")
    self.WTButtonSensors.child.modify_font(pango.FontDescription(FontButton))
    self.WTButtonSensors.child.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("red"))
    
    self.WTButtonPlusMin = wTree.get_widget("WTButtonPlusMin")
    self.WTButtonPlusMin.set_label(PlusMin)
    self.WTButtonPlusMin.connect("pressed",self.WTButtonPlusMin_clicked_cb,"")
    self.WTButtonPlusMin.child.modify_font(pango.FontDescription(FontButton))

    self.WTButtonSettings = wTree.get_widget("WTButtonSettings")
    self.WTButtonSettings.connect("pressed",self.WTButtonSettings_clicked_cb,"")
    self.WTButtonSettings.child.modify_font(pango.FontDescription(FontButton))

    self.WTLabelGPS = wTree.get_widget("WTLabelGPS")
    self.WTLabelGPS.modify_font(pango.FontDescription(FontGPSSmall))
    self.WTLabelGPS.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))

    self.WTLabelWindowTopBig = wTree.get_widget("WTLabelWindowTopBig")
    self.WTLabelWindowTopBig.set_label(str(Interval))
    self.WTLabelWindowTopBig.modify_font(pango.FontDescription(FontButtonNumber))
    self.WTLabelWindowTopBig.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
     
    self.WTLabelWindowBottomBig = wTree.get_widget("WTLabelWindowBottomBig")
    self.WTLabelWindowBottomBig.set_label(str(Total/1000))
    self.WTLabelWindowBottomBig.modify_font(pango.FontDescription(FontButtonNumber))
    self.WTLabelWindowBottomBig.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    
    self.WTButtonLog1 = wTree.get_widget("WTButtonLog1")
    self.WTButtonLog1.connect("pressed",self.WTButtonLog1_clicked_cb,"")
    self.WTButtonLog1.child.modify_font(pango.FontDescription(FontLog))

    self.WTButtonLog2 = wTree.get_widget("WTButtonLog2")
    self.WTButtonLog2.connect("pressed",self.WTButtonLog2_clicked_cb,"")
    self.WTButtonLog2.child.modify_font(pango.FontDescription(FontLog))

    self.WTButtonLog3 = wTree.get_widget("WTButtonLog3")
    self.WTButtonLog3.connect("pressed",self.WTButtonLog3_clicked_cb,"")
    self.WTButtonLog3.child.modify_font(pango.FontDescription(FontLog))

    self.WTButtonLog4 = wTree.get_widget("WTButtonLog4")
    self.WTButtonLog4.connect("pressed",self.WTButtonLog4_clicked_cb,"")
    self.WTButtonLog4.child.modify_font(pango.FontDescription(FontLog))

    self.WTButtonLog5 = wTree.get_widget("WTButtonLog5")
    self.WTButtonLog5.connect("pressed",self.WTButtonLog5_clicked_cb,"")
    self.WTButtonLog5.child.modify_font(pango.FontDescription(FontLog))

    self.WTButtonLog6 = wTree.get_widget("WTButtonLog6")
    self.WTButtonLog6.connect("pressed",self.WTButtonLog6_clicked_cb,"")
    self.WTButtonLog6.child.modify_font(pango.FontDescription(FontLog))

    self.WTButtonLog7 = wTree.get_widget("WTButtonLog7")
    self.WTButtonLog7.connect("pressed",self.WTButtonLog7_clicked_cb,"")
    self.WTButtonLog7.child.modify_font(pango.FontDescription(FontLog))

    self.WTButtonLog8 = wTree.get_widget("WTButtonLog8")
    self.WTButtonLog8.connect("pressed",self.WTButtonLog8_clicked_cb,"")
    self.WTButtonLog8.child.modify_font(pango.FontDescription(FontLog))

    self.WTButtonLog9 = wTree.get_widget("WTButtonLog9")
    self.WTButtonLog9.connect("pressed",self.WTButtonLog9_clicked_cb,"")
    self.WTButtonLog9.child.modify_font(pango.FontDescription(FontLog))

    self.WTButtonLog10 = wTree.get_widget("WTButtonLog10")
    self.WTButtonLog10.connect("pressed",self.WTButtonLog10_clicked_cb,"")
    self.WTButtonLog10.child.modify_font(pango.FontDescription(FontLog))
    

# WTS
   
    self.WTS = wTree.get_widget("WindowTrucktripSettings")
    self.WTS.connect( "delete_event", self.QuitProgram)
    self.WTS.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
    self.WTS.set_resizable(False)
    self.WTS.set_geometry_hints(None, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, -1, -1, -1, -1)
 
    self.WTSButtonExit = wTree.get_widget("WTSButtonExit")
    self.WTSButtonExit.connect("pressed",self.WTSButtonExit_clicked_cb,"")
    self.WTSButtonExit.child.modify_font(pango.FontDescription(FontButton))
 
    self.WTSButtonCalibrate1 = wTree.get_widget("WTSButtonCalibrate1")
    self.WTSButtonCalibrate1.connect("pressed",self.WTSButtonCalibrate1_clicked_cb,"")
    self.WTSButtonCalibrate1.child.modify_font(pango.FontDescription(FontButton))

    self.WTSButtonCalibrate2 = wTree.get_widget("WTSButtonCalibrate2")
    self.WTSButtonCalibrate2.connect("pressed",self.WTSButtonCalibrate2_clicked_cb,"")
    self.WTSButtonCalibrate2.child.modify_font(pango.FontDescription(FontButton))

    self.WTSButtonProbe = wTree.get_widget("WTSButtonProbe")
    self.WTSButtonProbe.connect("pressed",self.WTSButtonProbe_clicked_cb,"")
    self.WTSButtonProbe.child.modify_font(pango.FontDescription(FontButton))

    self.WTSButtonSetInterval = wTree.get_widget("WTSButtonSetInterval")
    self.WTSButtonSetInterval.connect("pressed",self.WTSButtonSetInterval_clicked_cb,"")
    self.WTSButtonSetInterval.child.modify_font(pango.FontDescription(FontButton))

    self.WTSButtonSetTotal = wTree.get_widget("WTSButtonSetTotal")
    self.WTSButtonSetTotal.connect("pressed",self.WTSButtonSetTotal_clicked_cb,"")
    self.WTSButtonSetTotal.child.modify_font(pango.FontDescription(FontButton))

    self.WTSButtonSetSuperTotal = wTree.get_widget("WTSButtonSetSuperTotal")
    self.WTSButtonSetSuperTotal.connect("pressed",self.WTSButtonSetSuperTotal_clicked_cb,"")
    self.WTSButtonSetSuperTotal.child.modify_font(pango.FontDescription(FontButton))

    self.WTSButtonBack = wTree.get_widget("WTSButtonBack")
    self.WTSButtonBack.connect("pressed",self.WTSButtonBack_clicked_cb,"")
    self.WTSButtonBack.child.modify_font(pango.FontDescription(FontButton))

# WK
    self.WK = wTree.get_widget("WindowKeyboard")
    self.WK.connect( "delete_event", self.QuitProgram)
    self.WK.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
    self.WK.set_resizable(False)
    self.WK.set_geometry_hints(None, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, -1, -1, -1, -1)

    self.WKButtonReset = wTree.get_widget("WKButtonReset")
    self.WKButtonReset.connect("pressed",self.WKButtonReset_clicked_cb,"")
    self.WKButtonReset.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonSet = wTree.get_widget("WKButtonSet")
    self.WKButtonSet.connect("pressed",self.WKButtonSet_clicked_cb,"")
    self.WKButtonSet.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonBack = wTree.get_widget("WKButtonBack")
    self.WKButtonBack.connect("pressed",self.WKButtonBack_clicked_cb,"")
    self.WKButtonBack.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonPoint = wTree.get_widget("WKButtonPoint")
    self.WKButtonPoint.connect("pressed",self.WKButtonPoint_clicked_cb,"")
    self.WKButtonPoint.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonZero = wTree.get_widget("WKButtonZero")
    self.WKButtonZero.connect("pressed",self.WKButtonZero_clicked_cb,"")
    self.WKButtonZero.child.modify_font(pango.FontDescription(FontButton))
    
    self.WKButtonOne = wTree.get_widget("WKButtonOne")
    self.WKButtonOne.connect("pressed",self.WKButtonOne_clicked_cb,"")
    self.WKButtonOne.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonTwo = wTree.get_widget("WKButtonTwo")
    self.WKButtonTwo.connect("pressed",self.WKButtonTwo_clicked_cb,"")
    self.WKButtonTwo.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonThree = wTree.get_widget("WKButtonThree")
    self.WKButtonThree.connect("pressed",self.WKButtonThree_clicked_cb,"")
    self.WKButtonThree.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonFour = wTree.get_widget("WKButtonFour")
    self.WKButtonFour.connect("pressed",self.WKButtonFour_clicked_cb,"")
    self.WKButtonFour.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonFive = wTree.get_widget("WKButtonFive")
    self.WKButtonFive.connect("pressed",self.WKButtonFive_clicked_cb,"")
    self.WKButtonFive.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonSix = wTree.get_widget("WKButtonSix")
    self.WKButtonSix.connect("pressed",self.WKButtonSix_clicked_cb,"")
    self.WKButtonSix.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonSeven = wTree.get_widget("WKButtonSeven")
    self.WKButtonSeven.connect("pressed",self.WKButtonSeven_clicked_cb,"")
    self.WKButtonSeven.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonEight = wTree.get_widget("WKButtonEight")
    self.WKButtonEight.connect("pressed",self.WKButtonEight_clicked_cb,"")
    self.WKButtonEight.child.modify_font(pango.FontDescription(FontButton))

    self.WKButtonNine = wTree.get_widget("WKButtonNine")
    self.WKButtonNine.connect("pressed",self.WKButtonNine_clicked_cb,"")
    self.WKButtonNine.child.modify_font(pango.FontDescription(FontButton))

    self.WKLabelHeader = wTree.get_widget("WKLabelHeader")
    self.WKLabelHeader.set_label("Calibrate")
    self.WKLabelHeader.modify_font(pango.FontDescription(FontHeader))
    self.WKLabelHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))

    self.WKLabelNumber = wTree.get_widget("WKLabelNumber")
    self.WKLabelNumber.set_label("0")
    self.WKLabelNumber.modify_font(pango.FontDescription(FontFigure))
    self.WKLabelNumber.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))

#WSP
    self.WSP = wTree.get_widget("WindowSelectProbe")
    self.WSP.connect( "delete_event", self.QuitProgram)
    self.WSP.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
    self.WSP.set_resizable(False)
    self.WSP.set_geometry_hints(None, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, -1, -1, -1, -1)


    self.WSPButtonProbe1 = wTree.get_widget("WSPButtonProbe1")
    self.WSPButtonProbe1.connect("pressed",self.WSPButtonProbe1_clicked_cb,"")
    self.WSPButtonProbe1.child.modify_font(pango.FontDescription(FontButton))

    self.WSPButtonProbe2 = wTree.get_widget("WSPButtonProbe2")
    self.WSPButtonProbe2.connect("pressed",self.WSPButtonProbe2_clicked_cb,"")
    self.WSPButtonProbe2.child.modify_font(pango.FontDescription(FontButton))

    self.WSPButtonProbe3 = wTree.get_widget("WSPButtonProbe3")
    self.WSPButtonProbe3.connect("pressed",self.WSPButtonProbe3_clicked_cb,"")
    self.WSPButtonProbe3.child.modify_font(pango.FontDescription(FontButton))

    self.WSPButtonProbe4 = wTree.get_widget("WSPButtonProbe4")
    self.WSPButtonProbe4.connect("pressed",self.WSPButtonProbe4_clicked_cb,"")
    self.WSPButtonProbe4.child.modify_font(pango.FontDescription(FontButton))
 
    self.WSPButtonGPS = wTree.get_widget("WSPButtonGPS")
    self.WSPButtonGPS.connect("pressed",self.WSPButtonGPS_clicked_cb,"")
    self.WSPButtonGPS.child.modify_font(pango.FontDescription(FontButton))

    self.WSPButtonBack = wTree.get_widget("WSPButtonBack")
    self.WSPButtonBack.connect("pressed",self.WSPButtonBack_clicked_cb,"")
    self.WSPButtonBack.child.modify_font(pango.FontDescription(FontButton))

#WG
    self.WG = wTree.get_widget("WindowGPS")
    self.WG.connect( "delete_event", self.QuitProgram)
    self.WG.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
    self.WG.set_resizable(False)
    self.WG.set_geometry_hints(None, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, -1, -1, -1, -1)

    self.WGHeader = wTree.get_widget("WGHeader")

    self.WGLabelTime = wTree.get_widget("WGLabelTime")
    self.WGLabelDate = wTree.get_widget("WGLabelDate")
    self.WGLabelLatitude = wTree.get_widget("WGLabelLatitude")
    self.WGLabelLongitude = wTree.get_widget("WGLabelLongitude")
    self.WGLabelNumberSattelites = wTree.get_widget("WGLabelNumberSattelites")
    self.WGLabelSpeed = wTree.get_widget("WGLabelSpeed")
    self.WGLabelHeight = wTree.get_widget("WGLabelHeight")
    self.WGLabelCourse = wTree.get_widget("WGLabelCourse")

    self.WGLabelTimeHeader = wTree.get_widget("WGLabelTimeHeader")
    self.WGLabelTimeHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGLabelTimeHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGLabelDateHeader = wTree.get_widget("WGLabelDateHeader")
    self.WGLabelDateHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGLabelDateHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGLabelLatitudeHeader = wTree.get_widget("WGLabelLatitudeHeader")
    self.WGLabelLatitudeHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGLabelLatitudeHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGLabelLongitudeHeader = wTree.get_widget("WGLabelLongitudeHeader")
    self.WGLabelLongitudeHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGLabelLongitudeHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGLabelNumberSattelitesHeader = wTree.get_widget("WGLabelNumberSattelitesHeader")
    self.WGLabelNumberSattelitesHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGLabelNumberSattelitesHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGLabelSpeedHeader = wTree.get_widget("WGLabelSpeedHeader")
    self.WGLabelSpeedHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGLabelSpeedHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGLabelHeightHeader = wTree.get_widget("WGLabelHeightHeader")
    self.WGLabelHeightHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGLabelHeightHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGLabelCourseHeader = wTree.get_widget("WGLabelCourseHeader")
    self.WGLabelCourseHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGLabelCourseHeader.modify_font(pango.FontDescription(FontLabel))

    self.WGButtonGSC = wTree.get_widget("WGButtonGSC")
    self.WGButtonGSC.connect("pressed",self.WGButtonGSC_clicked_cb,"")
    self.WGButtonGSC.child.modify_font(pango.FontDescription(FontButton))

    self.WGButtonBack = wTree.get_widget("WGButtonBack")
    self.WGButtonBack.connect("pressed",self.WGButtonBack_clicked_cb,"")
    self.WGButtonBack.child.modify_font(pango.FontDescription(FontButton))

#WGSC
    self.WGSC = wTree.get_widget("WindowGPSSC")
    self.WGSC.connect( "delete_event", self.QuitProgram)
    self.WGSC.modify_bg(gtk.STATE_NORMAL, gtk.gdk.color_parse("black"))
    self.WGSC.set_resizable(False)
    self.WGSC.set_geometry_hints(None, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, ScreenWidth, ScreenHeight, -1, -1, -1, -1)

    self.WGSCHeader = wTree.get_widget("WGSCHeader")

    self.WGSCLabelTime = wTree.get_widget("WGSCLabelTime")
    self.WGSCLabelDate = wTree.get_widget("WGSCLabelDate")
    self.WGSCLabelLatitude = wTree.get_widget("WGSCLabelLatitude")
    self.WGSCLabelLongitude = wTree.get_widget("WGSCLabelLongitude")
    self.WGSCLabelNumberSattelites = wTree.get_widget("WGSCLabelNumberSattelites")
    self.WGSCLabelSpeed = wTree.get_widget("WGSCLabelSpeed")
    self.WGSCLabelHeight = wTree.get_widget("WGSCLabelHeight")
    self.WGSCLabelCourse = wTree.get_widget("WGSCLabelCourse")

    self.WGSCLabelTimeHeader = wTree.get_widget("WGSCLabelTimeHeader")
    self.WGSCLabelTimeHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGSCLabelTimeHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGSCLabelDateHeader = wTree.get_widget("WGSCLabelDateHeader")
    self.WGSCLabelDateHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGSCLabelDateHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGSCLabelLatitudeHeader = wTree.get_widget("WGSCLabelLatitudeHeader")
    self.WGSCLabelLatitudeHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGSCLabelLatitudeHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGSCLabelLongitudeHeader = wTree.get_widget("WGSCLabelLongitudeHeader")
    self.WGSCLabelLongitudeHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGSCLabelLongitudeHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGSCLabelNumberSattelitesHeader = wTree.get_widget("WGSCLabelNumberSattelitesHeader")
    self.WGSCLabelNumberSattelitesHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGSCLabelNumberSattelitesHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGSCLabelSpeedHeader = wTree.get_widget("WGSCLabelSpeedHeader")
    self.WGSCLabelSpeedHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGSCLabelSpeedHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGSCLabelHeightHeader = wTree.get_widget("WGSCLabelHeightHeader")
    self.WGSCLabelHeightHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGSCLabelHeightHeader.modify_font(pango.FontDescription(FontLabel))
    self.WGSCLabelCourseHeader = wTree.get_widget("WGSCLabelCourseHeader")
    self.WGSCLabelCourseHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))
    self.WGSCLabelCourseHeader.modify_font(pango.FontDescription(FontLabel))

    self.WGSCButtonG = wTree.get_widget("WGSCButtonG")
    self.WGSCButtonG.connect("pressed",self.WGSCButtonG_clicked_cb,"")
    self.WGSCButtonG.child.modify_font(pango.FontDescription(FontButton))

    self.WGSCButtonBack = wTree.get_widget("WGSCButtonBack")
    self.WGSCButtonBack.connect("pressed",self.WGSCButtonBack_clicked_cb,"")
    self.WGSCButtonBack.child.modify_font(pango.FontDescription(FontButton))





    self.ShowHeader()


    if BasicWindow == "WS":
       self.RefreshScreen()
       self.WS.set_modal(True)
       self.WS.show()
       self.WS.present()
    if BasicWindow == "WT":
       self.RefreshScreen()
       self.WT.set_modal(True)
       self.WT.show()
       self.WT.present()
    if BasicWindow == "WG":
       self.RefreshScreen()
       self.WG.set_modal(True)
       self.WG.show()
       self.WG.present()
    if BasicWindow == "WGSC":
       self.RefreshScreen()
       self.WGSC.set_modal(True)
       self.WGSC.show()
       self.WGSC.present()


   def ShowHeader(self):
    Header = "-- Sensors " + time.strftime('%X %x')
    if Probe == "GPS":
       Header = Header + " -- Probe GPS --"
    else:
       Header = Header + " -- Probe " + Probe + " -- Calibration " + Calibration + " --"
    self.WSHeader.set_text(Header)
    self.WSHeader.modify_font(pango.FontDescription(FontHeader))
    self.WSHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("orange"))

    Header = "-- GPS " + time.strftime('%X %x')
    if Probe <> "GPS":
       Header = Header + " -- Probe " + Probe + " -- Calibration " + Calibration + " --"
    self.WGHeader.set_text(Header)
    self.WGHeader.modify_font(pango.FontDescription(FontHeader))
    self.WGHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("orange"))
    self.WGSCHeader.set_text(Header)
    self.WGSCHeader.modify_font(pango.FontDescription(FontHeader))
    self.WGSCHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("orange"))

    Header = "-- TruckTrip " + time.strftime('%X %x')
    if Probe == "GPS":
       Header = Header + " -- Probe GPS --"
    else:
       Header = Header + " -- Probe " + Probe + " -- Calibration " + Calibration + " --"
    self.WTHeader.set_text(Header)
    self.WTHeader.modify_font(pango.FontDescription(FontHeader))
    self.WTHeader.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("orange"))

# WS
   def WSButtonSettings_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WSS.set_modal(True)
       self.RefreshScreen()    
       self.WSS.show()
       self.WSS.present()

   def WSButtonTrucktrip_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global BasicWindow
    BasicWindow = "WT"
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WT.set_modal(True)
       self.RefreshScreen()    
       self.WT.show()
       self.WT.present()

   def WSButtonGPS_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global BasicWindow
    BasicWindow = "WG"
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WG.set_modal(True)
       self.RefreshScreen()    
       self.WG.show()
       self.WG.present()

   def WSButtonGPSSC_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global BasicWindow
    BasicWindow = "WGSC"
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WGSC.set_modal(True)
       self.RefreshScreen()    
       self.WGSC.show()
       self.WGSC.present()


# WSS
   def WSSButtonBack_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WS.set_modal(True)
       self.RefreshScreen()    
       self.WS.show()
       self.WS.present()

   def WSSButtonExit_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App.SafeBasicSettings()
       gtk.main_quit()
       self.RefreshScreen()

   def WSSButtonUnityGPS_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global UnityGPS
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if UnityGPS == "degrees":
          UnityGPS = "decimals"
       else:
          UnityGPS = "degrees"
       self.RefreshScreen()

# WT
   def WTButtonTop_clicked_cb(self,arg1, arg2):
    global Interval
    global Total
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.FillLog(0)
       if self.WTButtonTop.get_label() == "Interval (m)":
          App2.LogTrucktrip("I")
          Interval = 0.0
       if self.WTButtonTop.get_label() == "Total (km)":
          App2.LogTrucktrip("T")
          Total = 0.0
       os.system("aplay -q "+SoundReset)
       self.RefreshScreen()    

   def WTButtonBottom_clicked_cb(self,arg1, arg2):
    global Total
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.FillLog(0)
       if self.WTButtonBottom.get_label() == "Total (km)":
          App2.LogTrucktrip("T")
          Total = 0.0
       os.system("aplay -q "+SoundReset)
       self.RefreshScreen()    

   def WTButtonDisplay_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global Display
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if Display == "Display I/S":
          Display = "Display T/S"
       else:
          if Display == "Display T/S":
             Display = "Display I/T"
          else:
             Display = "Display I/S"
       self.RefreshScreen()    
       self.WT.show()
       self.WT.present()

   def WTButtonSensors_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global BasicWindow
    BasicWindow = "WS"
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WS.set_modal(True)
       self.RefreshScreen()    
       self.WS.show()
       self.WS.present()
    
   def WTButtonSettings_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WTS.set_modal(True)
       self.RefreshScreen()      
       self.WTS.show()
       self.WTS.present()

   def WTButtonPlusMin_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global PlusMin
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if PlusMin == "Plus>Min":
          PlusMin = "Min>Plus"
       else:
          PlusMin = "Plus>Min"
       self.RefreshScreen()
       self.WTButtonPlusMin.show()

   def WTButtonLog1_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L1")
       self.FillLog(1)
       self.RefreshScreen()
   def WTButtonLog2_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L2")
       self.FillLog(2)
       self.RefreshScreen()
   def WTButtonLog3_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L3")
       self.FillLog(3)
       self.RefreshScreen()
   def WTButtonLog4_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L4")
       self.FillLog(4)
       self.RefreshScreen()
   def WTButtonLog5_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L5")
       self.FillLog(5)
       self.RefreshScreen()
   def WTButtonLog6_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L6")
       self.FillLog(6)
       self.RefreshScreen()
   def WTButtonLog7_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L7")
       self.FillLog(7)
       self.RefreshScreen()
   def WTButtonLog8_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L8")
       self.FillLog(8)
       self.RefreshScreen()
   def WTButtonLog9_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L9")
       self.FillLog(9)
       self.RefreshScreen()
   def WTButtonLog10_clicked_cb(self,arg1,arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App2.LogTrucktrip("L10")
       self.FillLog(10)
       self.RefreshScreen()

# WTS
   def WTSButtonExit_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       App.SafeBasicSettings()
       gtk.main_quit()
       self.RefreshScreen()

   def WTSButtonCalibrate1_clicked_cb(self,arg1, arg2):
    global Calibration1
    global Calibration
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       Calibration = "1"
       self.WKLabelHeader.set_label("Calibrate 1")
       if long(Calibration1) == Calibration1:
          self.WKLabelNumber.set_label(str(long(Calibration1)))
       else:
          self.WKLabelNumber.set_label(str(Calibration1))
       self.WK.set_modal(True)
       self.RefreshScreen()    
       self.WK.show()
       self.WK.present()

   def WTSButtonCalibrate2_clicked_cb(self,arg1, arg2):
    global Calibration2
    global Calibration
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       Calibration = "2"
       self.WKLabelHeader.set_label("Calibrate 2")
       if long(Calibration2) == Calibration2:
          self.WKLabelNumber.set_label(str(long(Calibration2)))
       else:
          self.WKLabelNumber.set_label(str(Calibration2))
       self.WK.set_modal(True)
       self.RefreshScreen()    
       self.WK.show()
       self.WK.present()
    
   def WTSButtonProbe_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       self.HideWindows()
       KeyboardTimeOld = KeyboardTimeNew
       self.WSP.set_modal(True)
       self.RefreshScreen()    
       self.WSP.show()
       self.WSP.present()  
    
   def WTSButtonSetInterval_clicked_cb(self,arg1, arg2):
    global Interval
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WKLabelHeader.set_label("Set Interval")
       self.WKLabelNumber.set_label("%.0f" % Interval)
       #if long(Interval) == Interval:
          #self.WKLabelNumber.set_label(str(long(Interval)))
       #else:
          #self.WKLabelNumber.set_label(str(Interval))
       self.WK.set_modal(True)
       self.RefreshScreen()    
       self.WK.show()
       self.WK.present()
    
   def WTSButtonSetTotal_clicked_cb(self,arg1, arg2):
    global Total
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WKLabelHeader.set_label("Set Total")
       self.WKLabelNumber.set_label("%.2f" % (Total/1000))
       self.WK.set_modal(True)
       self.RefreshScreen()    
       self.WK.show()
       self.WK.present()
    
   def WTSButtonSetSuperTotal_clicked_cb(self,arg1, arg2):
    global SuperTotal
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WKLabelHeader.set_label("Set Super Total")
       self.WKLabelNumber.set_label("%.2f" % (SuperTotal/1000))
       self.WK.set_modal(True)
       self.RefreshScreen()    
       self.WK.show()
       self.WK.present()

   def WTSButtonBack_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WT.set_modal(True)
       self.RefreshScreen()    
       self.WT.show()
       self.WT.present()

# WK
   def WKButtonReset_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.WKLabelNumber.set_label("0")
       self.RefreshScreen()    

   def WKButtonSet_clicked_cb(self,arg1, arg2):
    global Calibration1
    global Calibration2
    global Interval
    global Total
    global SuperTotal
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if self.WKLabelHeader.get_label() == "Calibrate 1":
          Calibration1 = int(self.WKLabelNumber.get_label())
       if self.WKLabelHeader.get_label() == "Calibrate 2":
          Calibration2 = int(self.WKLabelNumber.get_label())
       if self.WKLabelHeader.get_label() == "Set Interval":
          Interval = long(self.WKLabelNumber.get_label())
       if self.WKLabelHeader.get_label() == "Set Total":
          Total = float(self.WKLabelNumber.get_label())*1000
       if self.WKLabelHeader.get_label() == "Set Super Total":
          SuperTotal = float(self.WKLabelNumber.get_label())*1000
       self.HideWindows()
       self.WTS.set_modal(True)
       self.RefreshScreen()
       self.WTS.show()
       self.WTS.present()

   def WKButtonBack_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WTS.set_modal(True)
       self.RefreshScreen() 
       self.WTS.show()
       self.WTS.present()

   def WKButtonPoint_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() <> "_":
             if self.WKLabelNumber.get_label().find(".") == -1:
                self.WKLabelNumber.set_label(self.WKLabelNumber.get_label() + ".")
    self.RefreshScreen()    
   def WKButtonZero_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("0")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"0")
    self.RefreshScreen()    
   def WKButtonOne_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("1")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"1")
    self.RefreshScreen()    
   def WKButtonTwo_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("2")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"2")
    self.RefreshScreen()    
   def WKButtonThree_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("3")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"3")
    self.RefreshScreen()    
   def WKButtonFour_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("4")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"4")
    self.RefreshScreen()    
   def WKButtonFive_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("5")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"5")
    self.RefreshScreen()    
   def WKButtonSix_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("6")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"6")
    self.RefreshScreen()    
   def WKButtonSeven_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("7")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"7")
    self.RefreshScreen()    
   def WKButtonEight_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("8")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"8")
    self.RefreshScreen()    

   def WKButtonNine_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       if len(self.WKLabelNumber.get_label()) < self.WKLabelNumber.get_max_width_chars():
          if self.WKLabelNumber.get_label() == "0":
             self.WKLabelNumber.set_label("9")
          else:
             self.WKLabelNumber.set_label(self.WKLabelNumber.get_label()+"9")
    self.RefreshScreen()    

#WSP
   def WSPButtonBack_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WTS.set_modal(True)
       self.RefreshScreen() 
       self.WTS.show()
       self.WTS.present()

   def WSPButtonProbe1_clicked_cb(self,arg1, arg2):
    global Probe
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       Probe = "1"
       self.WTS.set_modal(True)
       self.RefreshScreen() 
       self.WTS.show()
       self.WTS.present()

   def WSPButtonProbe2_clicked_cb(self,arg1, arg2):
    global Probe
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       Probe = "2"
       self.WTS.set_modal(True)
       self.RefreshScreen() 
       self.WTS.show()
       self.WTS.present()

   def WSPButtonProbe3_clicked_cb(self,arg1, arg2):
    global Probe
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       Probe = "3"
       self.WTS.set_modal(True)
       self.RefreshScreen() 
       self.WTS.show()
       self.WTS.present()

   def WSPButtonProbe4_clicked_cb(self,arg1, arg2):
    global Probe
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       Probe = "4"
       self.WTS.set_modal(True)
       self.RefreshScreen() 
       self.WTS.show()
       self.WTS.present()

   def WSPButtonGPS_clicked_cb(self,arg1, arg2):
    global Probe
    global KeyboardTimeNew
    global KeyboardTimeOld
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       Probe = "GPS"
       self.WTS.set_modal(True)
       self.RefreshScreen() 
       self.WTS.show()
       self.WTS.present()


# WG
   def WGButtonBack_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global BasicWindow
    BasicWindow = "WS"
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WS.set_modal(True)
       self.RefreshScreen()    
       self.WS.show()
       self.WS.present()

   def WGButtonGSC_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global BasicWindow
    BasicWindow = "WGSC"
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WGSC.set_modal(True)
       self.RefreshScreen()    
       self.WGSC.show()
       self.WGSC.present()



# WGSC
   def WGSCButtonBack_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global BasicWindow
    BasicWindow = "WS"
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WS.set_modal(True)
       self.RefreshScreen()    
       self.WS.show()
       self.WS.present()

   def WGSCButtonG_clicked_cb(self,arg1, arg2):
    global KeyboardTimeNew
    global KeyboardTimeOld
    global BasicWindow
    BasicWindow = "WG"
    KeyboardTimeNew=time.time()
    if (KeyboardTimeNew - KeyboardTimeOld) > DelayKeyboard:
       KeyboardTimeOld = KeyboardTimeNew
       self.HideWindows()
       self.WG.set_modal(True)
       self.RefreshScreen()    
       self.WG.show()
       self.WG.present()


   def HideWindows(self):
    self.WS.set_modal(False)
    self.WSS.set_modal(False)
    self.WT.set_modal(False)
    self.WTS.set_modal(False)
    self.WSP.set_modal(False)
    self.WK.set_modal(False)
    self.WG.set_modal(False)
    self.WGSC.set_modal(False)
    
   def ReadGPS(self):
    global GPSTime
    global GPSDate
    global GPSLatitude
    global GPSLongitude
    global GPSNumberSattelites
    global GPSSpeed
    global GPSHeight
    global GPSCourse
    global GPSTime_
    global GPSDate_
    global GPSLatitude_
    global GPSLongitude_
    global GPSNumberSattelites_
    global GPSSpeed_
    global GPSHeight_
    global GPSCourse_
    global GPSCourseOld
    global GPSTimeNewGGA
    global GPSTimeOldGGA
    global GPSTimeNewRMC
    global GPSTimeOldRMC
    global GPSColorGGA
    global GPSColorRMC
    global GPSBuffer
    global GPSTimeNewLL
    global GPSTimeOldLL
    
    GPSTimeNewGGA = time.time()
    GPSTimeNewLL = time.time()
    NumberBytes = GPS.inWaiting()
    GPSList = GPS.read(NumberBytes).splitlines()
    # attach buffer from the ReadGPS before
    if GPSBuffer <> "":
       GPSList[0]=GPSBuffer+GPSList[0]

    for GPSLineComplete in GPSList:
     if GPSLineComplete.find("*")==-1:
        GPSBuffer = GPSLineComplete
        break
     else:
        GPSBuffer = ""
     GPSLine = GPSLineComplete.split(",")
     try:
      if GPSLine[0] == "$GPGGA":
         GPSTimeOldGGA = GPSTimeNewGGA
         GPSTime = GPSLine[1][0:2]+":"+GPSLine[1][2:4]+":"+GPSLine[1][4:6]
         GPSTime_ = float(GPSLine[1])
         if UnityGPS == "degrees":
            degrees = int(GPSLine[2][0:2])
            minutes = int(GPSLine[2][2:4])
            seconds = float(GPSLine[2][5:9])/10000*60
            GPSLatitude = GPSLine[3] + " " + str(degrees)+"-"+str(minutes)+"-%.2f" % seconds
         else:
            GPSLatitude = GPSLine[3] + " " + str(float(GPSLine[2]))
         GPSLatitude_ = float(GPSLine[2])
         if UnityGPS == "degrees":
            degrees = int(GPSLine[4][0:3])
            minutes = int(GPSLine[4][3:5])
            seconds = float(GPSLine[4][6:10])/10000*60
            GPSLongitude = GPSLine[5] + " " + str(degrees)+"-"+str(minutes)+"-%.2f" % seconds
         else:
            GPSLongitude = GPSLine[5] + " " + str(float(GPSLine[4]))
         GPSLongitude_ = float(GPSLine[4])
         GPSNumberSattelites = GPSLine[7]
         GPSNumberSattelites_ = float(GPSLine[7])
         GPSHeight = GPSLine[9] + " (m)"
         GPSHeight_ = float(GPSLine[9])
      if GPSLine[0] == "$GPRMC":      
         GPSTimeOldRMC = GPSTimeNewRMC
         GPSSpeed = str(int(float(GPSLine[7])*1.852)) + " (km/h)"
         GPSSpeed_ = float(GPSLine[7])*1.852
         if float(GPSLine[7])*1.852 > 5:
            GPSCourse = GPSLine[8] + " (degrees)"
            GPSCourseOld = GPSCourse
         else:
            GPSCourse = GPSCourseOld
         GPSCourse_ = float(GPSLine[8])
         GPSDate = GPSLine[9][0:2]+"/"+GPSLine[9][2:4]+"/20"+GPSLine[9][4:6]
         GPSDate_ = float(GPSLine[9])
     except:
      k=0
     if GPSTimeNewGGA - GPSTimeOldGGA < 5:
        GPSColorGGA = "white"
     else:
        if GPSTimeNewGGA - GPSTimeOldGGA < 20:
           GPSColorGGA = "orange"
        else:
           GPSColorGGA = "red"
     if GPSTimeNewRMC - GPSTimeOldRMC < 5:
        GPSColorRMC = "white"
     else:
        if GPSTimeNewRMC - GPSTimeOldRMC < 20:
           GPSColorRMC = "orange"
        else:
           GPSColorRMC = "red"

    # timeframe relevant or not. Of not leave procedure
    if GPSTimeOldLL - time.time() < DelayGPS:
        return True
    if GPSLatitudeOld_ == 0 or GPSLongitudeOld_ == 0:
        return True
    # If Probe = hall, leave this procedure
    if Probe == "1" or Probe == "2":
        return True

    Meters = DistanceGPS(GPSLatitude_, GPSLatitudeOld_, GPSLongitude_, GPSLongitudeOld_, True)
    Interval += Meters
    Total += Meters
    SuperTotal += Meters
    
    GPSLatitudeOld_ = GPSLatitude_
    GPSLongitudeOld_ = GPSLongitude_
    GPSTimeOldLL = GPSTimeNewLL

    # Speed calculation
    SpeedTimeNew = time.time()
    TimeFrame = SpeedTimeNew - SpeedTimeOld 
    if TimeFrame > DelaySpeed:
       Speed = float(Meters / TimeFrame / 1000 * 60 * 60) # convert from m/s to km/h
       SpeedTimeOld = SpeedTimeNew

    return True
       
   def DistanceGPS(self, Latitude1, Latitude2, Longitude1, Longitude2, Km):
    Latitude1 = radians(Latitude1)
    Latitude2 = radians(Latitude2)
    Longitude1 = radians(Longitude1)
    Longitude2 = radians(Longitude2)
    
    r = 6372.797 #mean radius earth
    DistanceLatitude = Latitude2 - Latitude1
    DistanceLongitude = Longitude2 - Longitude1
    
    a = sin((DistanceLatitude/2)**2)+cos(Latitude1)*cos(Latitude2)*sin((DistanceLongitude/2)**2)
    b = 2*atan2(sqrt(a),sqrt(1-a))
    
    if km:
       return float(r*c*1000)
    else:
       return float(r*c**1000*0.621371192) #miles
   
       
       
   def RefreshScreen(self): 
    global Interval
    global Total
    global Speed
    global Log
    global Display
    global PlusMin

    self.ShowHeader()

    if self.WS.get_modal():
        self.WS.present()
    if self.WSS.get_modal():
        self.WSS.present()
    if self.WT.get_modal():
        self.WT.present()
    if self.WTS.get_modal():
        self.WTS.present()
    if self.WSP.get_modal():
        self.WSP.present()
    if self.WK.get_modal():
        self.WK.present()
    if self.WG.get_modal():
        self.WG.present()
    if self.WGSC.get_modal():
        self.WGSC.present()

    if UnityGPS == "decimals":
       self.WSSButtonUnityGPS.set_label("GPS decimals")
    else:
       self.WSSButtonUnityGPS.set_label("GPS degrees")
    self.WSSButtonUnityGPS.child.modify_font(pango.FontDescription(FontButton))
    
    self.WTButtonPlusMin.set_label(PlusMin)
    self.WTButtonPlusMin.child.modify_font(pango.FontDescription(FontButton))
   
    # Display is already T/S, but show I/S
    if Display == "Display T/S":
       self.WTButtonTop.set_label("Interval (m)")
       self.WTButtonBottom.set_label("Speed (km/h)")
       self.WTButtonDisplay.set_label(Display)
       self.WTButtonBottom.set_sensitive(False)
    else:
       # Display is already I/T, but show T/S
       if Display == "Display I/T":
          self.WTButtonTop.set_label("Total (km)")
          self.WTButtonBottom.set_label("Speed (km/h)")
          self.WTButtonBottom.set_sensitive(False)
          self.WTButtonDisplay.set_label(Display)
       else:
          # Display is already I/S, but show I/T
          self.WTButtonTop.set_label("Interval (m)")
          self.WTButtonBottom.set_label("Total (km)")
          self.WTButtonBottom.set_sensitive(True)
          self.WTButtonDisplay.set_label(Display)

    self.WTButtonDisplay.child.modify_font(pango.FontDescription(FontButton))
    self.WTButtonTop.child.modify_font(pango.FontDescription(FontButtonSmall))
    self.WTButtonBottom.child.modify_font(pango.FontDescription(FontButtonSmall))

    self.WTLabelGPS.set_label(GPSLatitude+" -- "+GPSLongitude+" -- "+GPSCourse)
    self.WTLabelGPS.modify_font(pango.FontDescription(FontGPSSmall))
    self.WTLabelGPS.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse("white"))

    if self.WTButtonTop.get_label() == "Interval (m)":
       self.WTLabelWindowTopBig.set_text("%.0f" % Interval)
    if self.WTButtonTop.get_label() == "Total (km)":
       self.WTLabelWindowTopBig.set_text("%.2f" % (Total/1000))
    if self.WTButtonBottom.get_label() == "Total (km)":
       self.WTLabelWindowBottomBig.set_text("%.2f" % (Total/1000))
    if self.WTButtonBottom.get_label() == "Speed (km/h)":
       self.WTLabelWindowBottomBig.set_text("%.0f" % Speed)
    
    self.WTButtonLog1.set_label(Log[0])
    self.WTButtonLog1.child.modify_font(pango.FontDescription(FontLog))
    self.WTButtonLog2.set_label(Log[1])
    self.WTButtonLog2.child.modify_font(pango.FontDescription(FontLog))
    self.WTButtonLog3.set_label(Log[2])
    self.WTButtonLog3.child.modify_font(pango.FontDescription(FontLog))
    self.WTButtonLog4.set_label(Log[3])
    self.WTButtonLog4.child.modify_font(pango.FontDescription(FontLog))
    self.WTButtonLog5.set_label(Log[4])
    self.WTButtonLog5.child.modify_font(pango.FontDescription(FontLog))
    self.WTButtonLog6.set_label(Log[5])
    self.WTButtonLog6.child.modify_font(pango.FontDescription(FontLog))
    self.WTButtonLog7.set_label(Log[6])
    self.WTButtonLog7.child.modify_font(pango.FontDescription(FontLog))
    self.WTButtonLog8.set_label(Log[7])
    self.WTButtonLog8.child.modify_font(pango.FontDescription(FontLog))
    self.WTButtonLog9.set_label(Log[8])
    self.WTButtonLog9.child.modify_font(pango.FontDescription(FontLog))
    self.WTButtonLog10.set_label(Log[9])
    self.WTButtonLog10.child.modify_font(pango.FontDescription(FontLog))
   
    self.meter1.set_text(App.GetBlock(1)[0])
    self.meter1.modify_font(pango.FontDescription(FontFigure))
    self.meter1.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(App.GetColor(1)))

    self.meter1textleft.set_text("")
    self.meter1textleft.modify_font(pango.FontDescription(FontDescription))
    self.meter1textright.set_text(App.GetBlock(1)[1] + "(" + App.GetBlock(1)[2] + ")")
    self.meter1textright.modify_font(pango.FontDescription(FontUnity))
    
    self.meter2.set_text (App.GetBlock(2)[0])
    self.meter2.modify_font(pango.FontDescription(FontFigure))
    self.meter2.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(App.GetColor(2)))

    self.meter2textleft.set_text ("")
    self.meter2textleft.modify_font(pango.FontDescription(FontDescription))
    self.meter2textright.set_text (App.GetBlock(2)[1] + "(" + App.GetBlock(2)[2] + ")")
    self.meter2textright.modify_font(pango.FontDescription(FontUnity))
    
    self.meter3.set_text (App.GetBlock(3)[0])
    self.meter3.modify_font(pango.FontDescription(FontFigure))
    self.meter3.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(App.GetColor(3)))

    self.meter3textleft.set_text ("")
    self.meter3textleft.modify_font(pango.FontDescription(FontDescription))
    self.meter3textright.set_text (App.GetBlock(3)[1] + "(" + App.GetBlock(3)[2] +")")
    self.meter3textright.modify_font(pango.FontDescription(FontUnity))
    
    self.meter4.set_text (App.GetBlock(4)[0])
    self.meter4.modify_font(pango.FontDescription(FontFigure))
    self.meter4.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(App.GetColor(4)))

    self.meter4textleft.set_text ("")
    self.meter4textleft.modify_font(pango.FontDescription(FontDescription))
    self.meter4textright.set_text (App.GetBlock(4)[1] + "(" + App.GetBlock(4)[2] + ")")
    self.meter4textright.modify_font(pango.FontDescription(FontUnity))
    
    self.meter5.set_text (App.GetBlock(5)[0])
    self.meter5.modify_font(pango.FontDescription(FontFigure))
    self.meter5.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(App.GetColor(5)))

    self.meter5textleft.set_text ("")
    self.meter5textleft.modify_font(pango.FontDescription(FontDescription))
    self.meter5textright.set_text (App.GetBlock(5)[1] + "(" + App.GetBlock(5)[2] + ")")
    self.meter5textright.modify_font(pango.FontDescription(FontUnity))
    
    self.meter6.set_text (App.GetBlock(6)[0])
    self.meter6.modify_font(pango.FontDescription(FontFigure))
    self.meter6.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(App.GetColor(6)))

    self.meter6textleft.set_text ("")
    self.meter6textleft.modify_font(pango.FontDescription(FontDescription))
    self.meter6textright.set_text (App.GetBlock(6)[1] + "(" + App.GetBlock(6)[2] + ")")
    self.meter6textright.modify_font(pango.FontDescription(FontUnity))

#WG    
    self.WGLabelTime.set_text(GPSTime)
    self.WGLabelTime.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGLabelTime.modify_font(pango.FontDescription(FontGPSSmall))
    self.WGLabelDate.set_text(GPSDate)
    self.WGLabelDate.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorRMC))
    self.WGLabelDate.modify_font(pango.FontDescription(FontGPSSmall))

    self.WGLabelLatitude.set_text(GPSLatitude)
    self.WGLabelLatitude.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGLabelLatitude.modify_font(pango.FontDescription(FontGPSBig))
    self.WGLabelLongitude.set_text(GPSLongitude)
    self.WGLabelLongitude.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGLabelLongitude.modify_font(pango.FontDescription(FontGPSBig))

    self.WGLabelNumberSattelites.set_text(GPSNumberSattelites)
    self.WGLabelNumberSattelites.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGLabelNumberSattelites.modify_font(pango.FontDescription(FontGPSSmall))
    self.WGLabelSpeed.set_text(GPSSpeed)
    self.WGLabelSpeed.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorRMC))
    self.WGLabelSpeed.modify_font(pango.FontDescription(FontGPSSmall))
    self.WGLabelHeight.set_text(GPSHeight)
    self.WGLabelHeight.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGLabelHeight.modify_font(pango.FontDescription(FontGPSSmall))
    self.WGLabelCourse.set_text(GPSCourse)
    self.WGLabelCourse.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorRMC))
    self.WGLabelCourse.modify_font(pango.FontDescription(FontGPSSmall))

#WGSC
    self.WGSCLabelNumberSattelites.set_text(GPSNumberSattelites)
    self.WGSCLabelNumberSattelites.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGSCLabelNumberSattelites.modify_font(pango.FontDescription(FontGPSSmall))
    self.WGSCLabelSpeed.set_text(GPSSpeed)
    self.WGSCLabelSpeed.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorRMC))
    self.WGSCLabelSpeed.modify_font(pango.FontDescription(FontGPSBig))
    self.WGSCLabelHeight.set_text(GPSHeight)
    self.WGSCLabelHeight.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGSCLabelHeight.modify_font(pango.FontDescription(FontGPSSmall))
    self.WGSCLabelCourse.set_text(GPSCourse)
    self.WGSCLabelCourse.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorRMC))
    self.WGSCLabelCourse.modify_font(pango.FontDescription(FontGPSBig))

    self.WGSCLabelTime.set_text(GPSTime)
    self.WGSCLabelTime.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGSCLabelTime.modify_font(pango.FontDescription(FontGPSSmall))
    self.WGSCLabelDate.set_text(GPSDate)
    self.WGSCLabelDate.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorRMC))
    self.WGSCLabelDate.modify_font(pango.FontDescription(FontGPSSmall))

    self.WGSCLabelLatitude.set_text(GPSLatitude)
    self.WGSCLabelLatitude.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGSCLabelLatitude.modify_font(pango.FontDescription(FontGPSSmall))
    self.WGSCLabelLongitude.set_text(GPSLongitude)
    self.WGSCLabelLongitude.modify_fg(gtk.STATE_NORMAL, gtk.gdk.color_parse(GPSColorGGA))
    self.WGSCLabelLongitude.modify_font(pango.FontDescription(FontGPSSmall))

    if not self.StopApplication:
        return True
    else:
        gtk.main_quit()

   def ReadOctopus(self):
    for line_ports in Ports:
        OctopusValue = float(octopus_adc_get(op, int(line_ports[3])))
        if line_ports[4] == "digital":
            if OctopusValue == 0:
                line_ports[7] = "ok"
            else:
                line_ports[7] = "bad"
        else:
            if OctopusValue > 0:
                line_ports[7] = float(OctopusValue*float(line_ports[5]))
            else:
                line_ports[7] = 0

    if not self.StopApplication:
        return True
    else:
        gtk.main_quit()

   def ReadArduino(self):
    global ArduinoBlocked

    for line_ports in Ports:
        ArduinoTime = time.time()
        if not ArduinoBlocked:
           ArduinoBlocked = True
           Arduino.write("$"+line_ports[3]+"#")
           ArduinoIn = Arduino.readline()
           ArduinoBlocked = False
        else:
           time.sleep(0.1)
           # retry after 100 ms
           if not ArduinoBlocked:
              ArduinoBlocked = True
              Arduino.write("$C##")
              ArduinoIn = Arduino.readline()
              ArduinoBlocked = False
           else:
              return True
        # strip "\n", "\r" and "!"
        ArduinoIn = ArduinoIn.rstrip('\n')
        ArduinoIn = ArduinoIn.rstrip('\r')
        if ArduinoIn == "$ERROR!":
           continue
        ArduinoIn = ArduinoIn.rstrip('!')
        ArduinoValue = float(ArduinoIn.rsplit('#')[1])
        if line_ports[4] == "digital":
            if line_ports[5] == 0:
               if ArduinoValue == 0: 
                   line_ports[7] = "ok"
               else:
                   line_ports[7] = "bad"
            else:
               if ArduinoValue <> 0: 
                   line_ports[7] = "ok"
               else:
                   line_ports[7] = "bad"
        else:
            ArduinoValue = float(ArduinoValue*float(line_ports[5]))
            if ArduinoValue > 0:
                if line_ports[4] != 'analog-conversions':
                   line_ports[7] = ArduinoValue
                else:
                    # use conversion table
                    for line_conversions in Conversions:
                        if line_conversions[0]==line_ports[0] and float(line_conversions[1]) <= ArduinoValue and ArduinoValue < float(line_conversions[2]):
                           line_ports[7] = float(line_conversions[3])
                           break
            else:
                line_ports[7] = 0

    if not self.StopApplication:
        return True
    else:
        gtk.main_quit()
        
        
   def ReadHallSensor(self):
    global Interval
    global Total
    global SuperTotal
    global ArduinoBlocked
    global Speed
    global SpeedTimeOld
    global SpeedTimeNew
    
    # If Probe = GPS, leave this procedure
    if Probe == "GPS":
        return True
    
    if not ArduinoBlocked:
       ArduinoBlocked = True
       Arduino.write("$C##")
       ArduinoIn = Arduino.readline()
       ArduinoBlocked = False
    else:
        time.sleep(0.1)
        # retry after 100 ms
        if not ArduinoBlocked:
           ArduinoBlocked = True
           Arduino.write("$C##")
           ArduinoIn = Arduino.readline()
           ArduinoBlocked = False
        else:
           return True

    # strip "\n", "\r" and "!"
    ArduinoIn = ArduinoIn.rstrip('\n')
    ArduinoIn = ArduinoIn.rstrip('\r')
    if ArduinoIn == "$ERROR!":
       return True
    ArduinoIn = ArduinoIn.rstrip('!')
    ArduinoValue = float(ArduinoIn.rsplit('#')[1])

    # also set the value of the WK screen if active and if calibrating
    if self.WK.get_modal:
       if self.WKLabelHeader.get_label() == "Calibrate 1" or self.WKLabelHeader.get_label() == "Calibrate 2":
          k = float(self.WKLabelNumber.get_label())
          k = k + ArduinoValue
          self.WKLabelNumber.set_label(str(long(k)))

    Meters = 0
    if Calibration == "1":
       Meters = float(ArduinoValue/Calibration1)*1000
    else:
       Meters = float(ArduinoValue/Calibration2)*1000
    Interval += Meters
    Total += Meters
    SuperTotal += Meters

    # Speed calculation
    SpeedTimeNew = time.time()
    TimeFrame = SpeedTimeNew - SpeedTimeOld 
    if TimeFrame > DelaySpeed:
       Speed = float(Meters / TimeFrame / 1000 * 60 * 60) # convert from m/s to km/h
       SpeedTimeOld = SpeedTimeNew
        
    if not self.StopApplication:
        return True
    else:
        gtk.main_quit()  
        
        
   def LogGPS(self):
    logfileGPS.write(time.strftime('%y%m%d'))
    logfileGPS.write(","+time.strftime('%H%M%S'))
    logfileGPS.write(","+"%.0f" % GPSDate_)
    logfileGPS.write(","+"%.0f" % GPSTime_)
    logfileGPS.write(","+"%.5f" % GPSLatitude_)
    logfileGPS.write(","+"%.5f" % GPSLongitude_)
    logfileGPS.write(","+"%.0f" % GPSNumberSattelites_)
    logfileGPS.write(","+"%.0f" % GPSHeight_)
    logfileGPS.write(","+"%.0f" % GPSSpeed_)
    logfileGPS.write(","+"%.0f" % GPSCourse_)

    for line_ports in Ports:
        logfileGPS.write(","+str(line_ports[7]))
        
    logfileGPS.writelines("\n")   
    return True

   def LogTrucktrip(self, Action):
    logfileTrucktrip.write(time.strftime('%y%m%d'))
    logfileTrucktrip.write(","+time.strftime('%H%M%S'))
    logfileTrucktrip.write(","+"%.0f" % Interval)
    logfileTrucktrip.write(","+"%.0f" % Total)
    logfileTrucktrip.write(","+"%.0f" % SuperTotal)
    logfileTrucktrip.write(","+"%.2f" % Speed)
    logfileTrucktrip.write(","+Probe)
    logfileTrucktrip.write(","+Calibration)    
    logfileTrucktrip.write(","+Action)
    logfileTrucktrip.writelines("\n")   
    return True

        
   def FillLog(self, position):
    global Log
    global Interval
    global Total
    # adding to the log, move whole the log 1 position forward
    if position == 0:
       i = 9
       while i > 0:
             Log[i] = Log[i-1]
# ?             root, root
             i = i - 1
       Log[0] = str(Interval) + " / %.2f" % float(Total/1000)
    # logbutton 1-10 is pressed, take value from log. When counting up, set Interval 0
    # when counting down, set interval to value
    if position <> 0:
       if self.WTButtonPlusMin.get_label() == "Plus>Min":
          Interval = 0
       else:
          Interval = float(Log[position-1].partition(" / ")[0])
       Total = float(Log[position-1].partition(" / ")[2])*1000
       
        
   def QuitProgram(self, widget, data=None):
    self.StopApplication = True
    
    
if __name__ == "__main__":
  global ScreenSettings
  global Warnings
  global Ports
  global Conversions
  global Sensors
  ScreenSettings = []
  Warnings = []
  Ports = [] 
  Conversions = []
  Sensors = []

  App = TruckPC() 

  # read ini file
  App.ReadSettings(FileSettings)

  # open octopus + basic settings
  if CheckOctopus:
     op=octopus_context()
     octopus_init(op)
     octopus_open(op)
     octopus_adc_ref(op, 2)
     for line_ports in Ports:
         octopus_adc_init(op, int(line_ports[3]))
         
  # open arduino
  if CheckArduino:       
     Arduino = serial.Serial(DeviceArduino, BaudrateArduino)
  
  # open serial port to GPS
  GPS = serial.Serial(DeviceGPS, BaudrateGPS)

  # open loggings
  # GPS log
  FileName = "LogGPS" + time.strftime('%Y%m%d') + ".log"
  if os.path.exists(FileName):
     logfileGPS = file(FileName, "a") 
  else:
     logfileGPS = file(FileName, "w") 
     logfileGPS.write("Date,Time,GPSDate,GPSTime,GPSLatitude,GPSLongitude,GPSNumberSattelites,GPSHeight,GPSSPeed,GPSCourse")
     for line_port in Ports:
         logfileGPS.write(","+line_port[0])
     logfileGPS.writelines("\n")
  # Trucktrip log
  FileName = "LogTrucktrip" + time.strftime('%Y%m%d') + ".log"
  if os.path.exists(FileName):
     logfileTrucktrip = file(FileName, "a") 
  else:
     logfileTrucktrip = file(FileName, "w") 
     logfileTrucktrip.write("Date,Time,Interval,Total,SuperTotal,Speed,Probe,Calibration,Action")
     logfileTrucktrip.writelines("\n")




  App2 = ShowWindow()
  App2.RefreshScreen()
  gobject.timeout_add(1000, App2.ReadGPS)
  gobject.timeout_add(2000, App2.RefreshScreen)
  gobject.timeout_add(1000, App2.ReadHallSensor)
  gobject.timeout_add(5000, App2.LogGPS)
  
  if CheckOctopus:
     gobject.timeout_add(500, App2.ReadOctopus)

  if CheckArduino:
     gobject.timeout_add(2500, App2.ReadArduino)
  
  gtk.main()
 
